﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ITSP.EventReceiverConfig
{

    class Program
    {
        private static string _siteUrl;
        private static ProgramControl.Mode _mode;

        private static string _eventreceivertargettype;
        private static string _eventreceiverassembly;
        private static string _eventreceiverclass;
        private static string _eventreceivertargetname;
        private static string _eventreceiversequence;
        private static string _eventreceivername;
        private static string _eventreceivertype;

        private static EventReceiverConfiguration _configuration = null;
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ProgramControl progControl = new ProgramControl();

            try
            {
                

                //check program mode
                if(args.Length < 1)
                {
                    throw new ArgumentException("No arguments specified.");
                }

                ProcessArguments(args);

                switch(_mode)
                {
                    case ProgramControl.Mode.List:
                        CheckForListMode();
                        break;
                    case ProgramControl.Mode.Add:
                        CheckForAddMode();
                        break;
                    case ProgramControl.Mode.Remove:
                        CheckForRemoveMode();
                        break;
                    default:
                        throw new ArgumentException(("-mode argument specified is not supported."));
                }


                int sequence = -1;
                if(!String.IsNullOrEmpty(_eventreceiversequence) &&  Int32.TryParse(_eventreceiversequence,out sequence))
                {
                     //keep value  
                }
                else
                {
                    sequence = 50000;
                }
                _configuration = new EventReceiverConfiguration(_eventreceiverassembly, _eventreceiverclass, _eventreceivername, sequence, _eventreceivertype, _eventreceivertargettype, _eventreceivertargetname);

                progControl.SetProgramMode(_mode);
                progControl.SetContext(_siteUrl);
                progControl.SetConfiguration(_configuration);
                progControl.DoTask();
            }
            catch (ArgumentException argex)
            {
                progControl.DisplayUsage();
                Console.WriteLine(argex.Message);
            }
            catch(Exception ex)
            {
                Console.WriteLine("An error occured: " + ex.Message);
            }
        }

        /// <summary>
        /// Check argument for Add operation mode.
        /// </summary>
        /// <returns></returns>
        static void CheckForAddMode()
        {
            bool bSuccess = false;

            if(String.IsNullOrEmpty(_eventreceiverassembly) || String.IsNullOrEmpty(_eventreceiverclass) || String.IsNullOrEmpty(_eventreceivername) || String.IsNullOrEmpty(_eventreceivertargetname) || String.IsNullOrEmpty(_eventreceivertargettype) || String.IsNullOrEmpty(_eventreceivertype))
            {
                throw new ArgumentException("One of the supplied arguments is empty please check parameters.");
            }

            bSuccess = true;

        }

        /// <summary>
        /// Check argument for List operation mode.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        static void CheckForListMode()
        {
            bool bSuccess = false;

            if (String.IsNullOrEmpty(_eventreceivertargetname) || String.IsNullOrEmpty(_eventreceivertargettype))
            {
                throw new ArgumentException("One of the supplied arguments is empty please check parameters");
            }


            bSuccess = true;

        }

        /// <summary>
        /// Check argument for Remove operation mode
        /// </summary>
        /// <returns></returns>
        static void CheckForRemoveMode()
        {
            bool bSuccess = false;

            if (String.IsNullOrEmpty(_eventreceiverassembly) || String.IsNullOrEmpty(_eventreceiverclass) || String.IsNullOrEmpty(_eventreceivertargetname) || String.IsNullOrEmpty(_eventreceivertargettype))
            {
                throw new ArgumentException("One of the supplied arguments is empty please check parameters");
            }

            bSuccess = true;
        }

        /// <summary>
        /// Resolve the program mode from the arguments 
        /// that have been specified
        /// </summary>
        /// <param name="modestring"></param>
        /// <returns></returns>
        static ProgramControl.Mode ResolveProgramMode(string modestring)
        {
            ProgramControl.Mode mode = ProgramControl.Mode.List;
            switch(modestring)
            {
                case "Add":
                    mode = ProgramControl.Mode.Add;
                    break;
                case "Remove":
                    mode = ProgramControl.Mode.Remove;
                    break;
                case "List":
                default:
                    mode = ProgramControl.Mode.List;
                    break;
            }
            return mode;
        }

        /// <summary>
        /// Check the argumnets
        /// </summary>
        /// <param name="args"></param>
        static void ProcessArguments(string[] args)
        {
            bool bSuccess = false;

            for(int argNo=0;argNo < args.Length;argNo++)
            {
                string argument = args[argNo];
                switch(argument)
                {
                    case "-url":
                        _siteUrl = args[argNo + 1];
                        break;
                    case "-mode":
                        _mode = ResolveProgramMode(args[argNo + 1]);
                        break;
                    case "-assembly":
                        _eventreceiverassembly = args[argNo + 1];
                        break;
                    case "-class":
                        _eventreceiverclass = args[argNo + 1];
                        break;
                    case "-sequence":
                        _eventreceiversequence = args[argNo + 1];
                        break;
                    case "-name":
                        _eventreceivername = args[argNo + 1];
                        break;
                    case "-targettype":
                        _eventreceivertargettype = args[argNo + 1];
                        break;
                    case "-targetname":
                        _eventreceivertargetname = args[argNo + 1];
                        break;
                    case "-type":
                        _eventreceivertype = args[argNo + 1];
                        break;
                }
            }

        }

    }
}


